home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / msoftapp.zip / GENERIC.C < prev    next >
C/C++ Source or Header  |  1993-06-01  |  6KB  |  186 lines

  1.  
  2. // Global variables at the beginning of GENERIC.C
  3.  
  4. static  HINSTANCE hInst;            // current instance
  5. static  FARPROC lpProcHorzList;     // ptr to the HorzList procedure
  6. static  FARPROC lpProcAbout;        // ptr to the About procedure
  7. static  FARPROC lpProcOrgProc;      // ptr to the original list box proc
  8. static  FARPROC lpProcSubClassProc; // ptr to the subclass list box proc
  9.  
  10. static  UINT    uScrollMessage;     // custom notification message
  11.  
  12. /////////////////////////////////////////////////////////////////
  13. // Added to the instance-initialization routine
  14.  
  15. uScrollMessage = RegisterWindowMessage( "MyScrollNotificationMsg" );
  16.  
  17. /////////////////////////////////////////////////////////////////
  18. // Replacement for the WM_COMMAND case
  19.  
  20. case WM_COMMAND:    // message: command from application menu
  21.     {
  22.     switch (wParam)
  23.         {
  24.         case IDM_HORZLIST:
  25.             {
  26.             lpProcHorzList = MakeProcInstance(
  27.                     (FARPROC)HorzListProc, hInst );
  28.             lpProcSubClassProc = MakeProcInstance(
  29.                     (FARPROC)WndListBoxProc, hInst );
  30.  
  31.             DialogBox( hInst, "HorzListBox",
  32.                     hWnd, lpProcHorzList );
  33.  
  34.             FreeProcInstance( lpProcSubClassProc );
  35.             FreeProcInstance( lpProcHorzList );
  36.             return 0;
  37.             }
  38.  
  39.         case IDM_ABOUT:
  40.             {
  41.             lpProcAbout = MakeProcInstance( About, hInst );
  42.  
  43.             DialogBox( hInst, "AboutBox", hWnd, lpProcAbout );
  44.  
  45.             FreeProcInstance( lpProcAbout );
  46.             return 0;
  47.             }
  48.  
  49.         default:
  50.             // Let Windows process it
  51.             return DefWindowProc( hWnd, message, wParam, lParam );
  52.         } // End of switch (wParam)
  53.     } // End of case WM_COMMAND:
  54.  
  55. /////////////////////////////////////////////////////////////////
  56. //  Horizontal-Scrolling List Box Dialog Procedure
  57.  
  58. extern  BOOL FAR PASCAL HorzListProc
  59. (
  60.         HWND            hDlg,
  61.         unsigned        message,
  62.         WORD            wParam,
  63.         LONG            lParam
  64. )
  65. {
  66. auto    HWND            hWndListBox;
  67. auto    char            buf[256];
  68.  
  69. if (message == uScrollMessage)
  70.     {
  71.     int     iMinPos,
  72.             iMaxPos,
  73.             iCurPos;
  74.  
  75.     hWndListBox = GetDlgItem( hDlg, ID_LIST );
  76.     GetScrollRange( hWndListBox, SB_HORZ, &iMinPos, &iMaxPos );
  77.     iCurPos = GetScrollPos( hWndListBox, SB_HORZ );
  78.     wsprintf( buf, "Min: %d, Cur: %d, Max: %d",
  79.             iMinPos, iCurPos, iMaxPos );
  80.     SetDlgItemText( hDlg, ID_TEXT, buf );
  81.     return 0;
  82.     }
  83.  
  84. switch (message)
  85.     {
  86.     case WM_INITDIALOG:
  87.         {
  88.         char    c;
  89.  
  90.         // Get the window handle of the list box
  91.         hWndListBox = GetDlgItem( hDlg, ID_LIST );
  92.  
  93.         // Grab the original list box window procedure
  94.         lpProcOrgProc = (FARPROC)GetWindowLong(
  95.                 hWndListBox, GWL_WNDPROC );
  96.  
  97.         // ... and replace it with our own!
  98.         SetWindowLong( hWndListBox, GWL_WNDPROC,
  99.                 (LONG)lpProcSubClassProc );
  100.  
  101.         // Initialize the list box with some sample data
  102.         for (c = 'A';  c <= 'Z';  c++)
  103.             {
  104.             // format a string
  105.             wsprintf( buf, "%c\tThis is a very long string "
  106.                     "to test the horizontal scrolling\t%c",
  107.                     c, c );
  108.  
  109.             // stick the string into the list box
  110.             SendMessage( hWndListBox, LB_ADDSTRING, 0,
  111.                     (LPARAM)(LPSTR)buf );
  112.             }
  113.  
  114.         // Enable the horizontal scroll bar
  115.         SendMessage( hWndListBox,
  116.                 LB_SETHORIZONTALEXTENT, 500, 0L );
  117.  
  118.         // Set the horizontal scroll bar range and position
  119.         SetScrollRange( hWndListBox, SB_HORZ, 0, 100, FALSE );
  120.         SetScrollPos( hWndListBox, SB_HORZ, 0, FALSE );
  121.  
  122.         // Force the display to initialize the display
  123.         PostMessage( hDlg, uScrollMessage, wParam, lParam );
  124.  
  125.         return TRUE;
  126.         }
  127.  
  128.     case WM_COMMAND:
  129.         if (wParam == IDOK
  130.         ||  wParam == IDCANCEL)
  131.             {
  132.             // Get the window handle to the list box
  133.             hWndListBox = GetDlgItem( hDlg, ID_LIST );
  134.  
  135.             // Reinstall the original list box procedure
  136.             SetWindowLong( hWndListBox, GWL_WNDPROC,
  137.                     (LONG)lpProcOrgProc );
  138.  
  139.             // ... and end the dialog
  140.             EndDialog( hDlg, TRUE );
  141.             return TRUE;
  142.             }
  143.         break;
  144.     }
  145. return FALSE;
  146. }
  147.  
  148.  
  149. /////////////////////////////////////////////////////////////////
  150. //  List Box Subclassing Window Procedure.
  151. //  This subclass procedure will notify the parent window
  152. //  whenever the Horizontal Scroll Bar is scrolled.
  153.  
  154. extern  long FAR PASCAL WndListBoxProc
  155. (
  156.         HWND            hWnd,
  157.         unsigned        wMsg,
  158.         WORD            wParam,
  159.         LONG            lParam
  160. )
  161. {
  162. switch (wMsg)
  163.     {
  164.     // When the scroll bar notifies the parent window
  165.     // (in this case the list box) of a horizontal
  166.     // scroll message, notify the parent of the list
  167.     // box, also.
  168.     case WM_HSCROLL:
  169.         {
  170.         HWND hParentWnd;
  171.  
  172.         // Get the handle to the parent of the list box
  173.         hParentWnd = GetWindowWord( hWnd, GWW_HWNDPARENT );
  174.  
  175.         // Post a special notification message to it.
  176.         PostMessage( hParentWnd, uScrollMessage,
  177.                 wParam, lParam );
  178.         }
  179.     }
  180.  
  181. // Allow all messages to be processed normally by the original
  182. // list box window procedure
  183. return CallWindowProc( lpProcOrgProc, hWnd, wMsg, wParam, lParam );
  184. }
  185.  
  186.